package com.ycwl.freetv.program;

import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ycwl.library.ParseUIFromJson;

public class Text implements ProgramInterface {
    final TextView view;
    public static class Builder {
        final Context context;
        final ParseUIFromJson.BaseNode baseNode;
        public Builder(Context context, ParseUIFromJson.BaseNode baseNode) {
            this.context = context.getApplicationContext();
            this.baseNode = baseNode;
        }


        Text build() {
            return new Text(this);
        }
    }

    private Text(Builder builder) {
        TextView textView = new TextView(builder.context);
        RelativeLayout.LayoutParams marginLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        marginLayoutParams.leftMargin = builder.baseNode.x.intValue();
        marginLayoutParams.topMargin = builder.baseNode.y.intValue();
        marginLayoutParams.width = builder.baseNode.width.intValue();
        marginLayoutParams.height = builder.baseNode.height.intValue();
        textView.setLayoutParams(marginLayoutParams);
        try {
            if (builder.baseNode.text_size != null) {
                textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, builder.baseNode.text_size.intValue());
            } else {
                textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 50);
            }
            if  (builder.baseNode.text_background_color != null && builder.baseNode.text_background_color.length() > 1 && builder.baseNode.text_background_color.startsWith("#")) {
                textView.setBackgroundColor(Color.parseColor(builder.baseNode.text_background_color));
            }
            if  (builder.baseNode.text_color != null && builder.baseNode.text_color.length() > 1 && builder.baseNode.text_color.startsWith("#")) {
                textView.setTextColor(Color.parseColor(builder.baseNode.text_color));
            }
        } catch (NumberFormatException e) {
            Toast.makeText(builder.context.getApplicationContext(), "文字设置颜色失效" + builder.baseNode.name, Toast.LENGTH_LONG).show();
        }

        textView.setText(builder.baseNode.content.toCharArray(),0,builder.baseNode.content.length());
        this.view = textView;
    }
    @Override
    public void play() {

    }

    @Override
    public void stop() {

    }

    @Override
    public void setTotalTime(int totalTime) {

    }

    @Override
    public void pause() {

    }

    @Override
    public View getView() {
        return this.view;
    }
}
